#include "Client.h"
#include "Convar.h"
#include "Render.h"
#include <GL/glfw.h>

sb3dRender *render;

sb3dConVar	*r_width;
sb3dConVar	*r_height;

sb3dConVar	*gl_clear;

bool sb3dRender::Init( void )
{
	r_width = cvars->GetVar( "width", "320", CVAR_READ_ONLY, "screen width" );
	r_height = cvars->GetVar( "height", "200", CVAR_READ_ONLY, "screen height" );
	
	gl_clear = cvars->GetVar( "gl_clear", "0", CVAR_ARCHIVE, "clearing screen after each frame" );
	
	if( !glfwOpenWindow( r_width->integer, r_height->integer, 0,0,0,0, 32,0, GLFW_WINDOW ))
	{
		return false;
	}
	
	return true;
}

bool sb3dRender::PreRender( bool clearScene )
{
	if( gl_clear->integer && clearScene /*&& cls.state != ca_cinematic*/ )
	{
		glClear( GL_COLOR_BUFFER_BIT );
	}
	
	return true;
}

void sb3dRender::Render()
{
	if(!PreRender(true))
		return;
		
	switch( cls.state )
	{
		case ca_disconnected:
		break;
		case ca_connecting:
		case ca_connected:
			//SCR_DrawPlaque();
		break;
		case ca_active:
			RenderView();
		break;
		case ca_cinematic:
			//SCR_DrawCinematic();
		break;
		default:
			//Host_Error( "SCR_UpdateScreen: bad cls.state\n" );
		break;
	}
		
	PostRender();
}

void sb3dRender::RenderView()
{
}

void sb3dRender::PostRender()
{
}

void sb3dRender::Clear( int bitMask )
{
	int bits;

	bits = GL_DEPTH_BUFFER_BIT;
	
	bits &= bitMask;

	//if( bits & GL_STENCIL_BUFFER_BIT )
	//	pglClearStencil( 128 );

	glClear( bits );
	
}

int sb3dRender::LoadTexture( const char *name, const unsigned char *buf, size_t size, int flags )
{
	unsigned int		i, hash;
	
	
	// NOTE: always return texnum as index in array or engine will stop work !!!!
	return i;
}